package com.example.myapplication.tack_14;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_14.data.ArrangeData;
import com.example.myapplication.tack_14.data.DetailsData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;

public class DetailsActivity extends AppCompatActivity {
    private ImageView exit;
    private ImageView imags;
    private TextView text;
    private TextView name;
    private TextView createTime;
    private EditText edi;
    private TextView faodu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_14_activity_details);
        Intent intent = getIntent();
        String id = intent.getStringExtra(Common.COMMON);
        init();
        exit_();
        getDerails(id);
        faodu_();
    }

    private void faodu_() {
        faodu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(edi.getText().toString()) || TextUtils.isEmpty(edi.getText().toString())){
                    Toast.makeText(DetailsActivity.this,"请输入你的评论",Toast.LENGTH_SHORT).show();
                }else{
                    edi.setText("");
                    Toast.makeText(DetailsActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailsActivity.this.finish();
            }
        });
    }

    private void getDerails(String id) {
        String url = "prod-api/api/garbage-classification/news/"+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, DetailsData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                DetailsData arrangeData = (DetailsData)obj;
                if(arrangeData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(DetailsActivity.this).load(OkHttpTool.URL+arrangeData.getData().getImgUrl()).into(imags);
                            String str = Html.fromHtml(arrangeData.getData().getContent()).toString();
                            text.setText(str.trim());
                            name.setText(arrangeData.getData().getAuthor());
                            createTime.setText(arrangeData.getData().getCreateTime());

                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        imags = findViewById(R.id.imags);
        text = findViewById(R.id.text);
        name = findViewById(R.id.name);
        createTime = findViewById(R.id.createTime);
        edi = findViewById(R.id.edi);
        faodu = findViewById(R.id.faodu);
    }
}